package com.personal.cuncunxing.module.player;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.VideoView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.personal.cuncunxing.R;
import com.personal.cuncunxing.common.OkHttpUtils;
import com.personal.cuncunxing.common.ResultCallback;
import com.personal.cuncunxing.common.StaticFiled;
import com.personal.cuncunxing.util.EncodeUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

public class PlayerActivity extends AppCompatActivity {

    private static final String TAG = "PlayerActivity";
    private VideoView player_vv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        player_vv = findViewById(R.id.player_vv);
        String playUrl = getIntent().getStringExtra("playUrl").substring(10);
        Log.d(TAG, "onCreate: " + playUrl);
        Base64Prasing(playUrl);
    }

    /**
     * 解析播放资源
     *
     * @param url
     */
    private void Base64Prasing(String url) {
        Map<Object, Object> json = new HashMap<>();
        json.put("playUrl", url);
        OkHttpUtils.getInstance(this).postAsyncHttp(StaticFiled.PARSING_VIDEO, json, new ResultCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String str) throws IOException {
                Log.d(TAG, "onResponse: " + str);
                JSONObject jsonObject = JSON.parseObject(str);
                Log.d(TAG, "onResponse: " + EncodeUtils.urlDecode(jsonObject.getString("data")));
                player_vv.setVideoPath(EncodeUtils.urlDecode(jsonObject.getString("data")));
                player_vv.start();
            }
        });
    }
}
